﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Web;
using YouHP.Model.Group;

namespace YouHP.Pages
{
    public class PrescriptionAll : BasePage
    {
        protected List<string> mPingYin = new List<string>();
       
        protected override void ShowPage()
        {
            base.ShowPage();
            if(ErrorCount > 0) return;
            
            HandleCommon();
            if(ErrorCount > 0) return;
            
            if(IsPost) HandlePost();
            else HandleGet();
        }

        protected List<TopicInfo> TopicDataListHottest;
        protected List<TopicInfo> TopicDataListLatest;
        protected List<UserInfo> UserDataListLatest;

        // 和当前登录用户使用治疗最接近的病友
        protected List<UserInfo> bestFitPatientsByTreatments;

        // 治疗当前登录用户所患疾病的常用治疗
        protected string diseaseName = string.Empty;
        protected List<TreatmentAndUsedCount> mostFrequentUsedTreatmentsOfDisease;

        //protected List<TreatmentInfo> TreatmentDataList;
        protected List<DiseasePrescriptionStat> DiseasePrescriptionStatList;
        protected List<DiseaseInfo> DiseaseLst;
        protected int TreatmentCount = 0;
        protected int TreatmentPageCount = 0 ;
        protected int TreatmentPageSize = 15;
        protected int TreatmentPageIndex = 1;
        protected string TreatmentPagingString;

        protected string ItemUrl = "/Treatment.aspx?id=";
        protected string ItemOperName = "查看";

        protected void HandleCommon()
        {
            // 用户已经登录，直接跳转到相关疾病的页面

            if (UserData != null && UserData.Id > 0)
            {
                PatientRecordInfo prInfo = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
                int diseaseId = prInfo.DiseaseId;
                if (diseaseId > 0)
                {
                    string url = string.Format("/DoctorList.aspx?diseaseid={0}", diseaseId);
                    this.Redirect(url);
                }
            }

            this.AddLocation("中药列表", "");
            DiseaseLst = DiseaseBLL.Instance.GetAllDiseaseByName("");
            this.SetTitle("中药列表");

            for (char c = 'A'; c <= 'Z'; c++)
            {
                mPingYin.Add(c.ToString());
            }
        }
        
        protected void HandlePost()
        {
            
        }

        protected void HandleGet()
        {
            int topiccnt = 8;
            TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(topiccnt);
            TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(topiccnt);
            UserDataListLatest = PatientBLL.Instance.GetLastestPatient(topiccnt);

            TreatmentPageIndex = RequestTools.GetQueryInt("page", 1);

            // 如果用户已经登录，获取和当前登录用户使用治疗最接近的病友列表
            if (UserData.Id > 0)
            {
                PatientRecordInfo prInfo = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
                if (prInfo.DiseaseId > 0)
                {
                    UserData.DiseaseId = prInfo.DiseaseId;

                    bestFitPatientsByTreatments = PatientTreatmentBLL.Instance.GetBestFitTreatmentsPatientList(prInfo.Id);

                    if (bestFitPatientsByTreatments != null && bestFitPatientsByTreatments.Count > 4)
                        bestFitPatientsByTreatments = bestFitPatientsByTreatments.GetRange(0, 4);

                    // 获得治疗当前登录用户所患疾病的常用治疗
                    mostFrequentUsedTreatmentsOfDisease =
                        DiseaseBLL.Instance.GetMostFrequentUsedTreatmentsOfDisease(prInfo.DiseaseId, 4);

                    diseaseName = DiseaseBLL.Instance.GetDiseaseInfo(prInfo.DiseaseId).Name;
                }

            }

            DiseasePrescriptionStatList = PrescriptionBLL.Instance.GetDiseasePrescriptionStat(100);
        }      
    }
}